import React, { PureComponent } from 'react';

import { Tabs } from 'antd';
import router from 'umi/router';
import _ from 'lodash';
import * as apis from '@/services/PriceMgr';

const { TabPane } = Tabs;

export default class CurrentStage extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      stages: [],
    };
  }

  componentDidMount() {
    if (this.props.match.params.project && this.props.orgStructure.data.length > 0) {
      this.getStages(this.props.orgStructure.data, this.props.match.params.project);
    }
  }

  componentWillReceiveProps(nextProps) {
    if (
      this.props.match.params.project !== nextProps.match.params.project ||
      this.props.orgStructure.data !== nextProps.orgStructure.data
    ) {
      if (nextProps.orgStructure.data.length > 0) {
        this.getStages(nextProps.orgStructure.data, nextProps.match.params.project);
      }
    }
  }

  getStages(orgStructure, project) {
    const stages = this.findOrgStructureChildren(orgStructure, project);
    this.setState({ stages });
  }

  findOrgStructureItem = (orgStructure, id) => {
    const filteredItems = _.filter(orgStructure, item => `${item.orgdepid}` === `${id}`);
    if (filteredItems && filteredItems.length > 0) {
      return filteredItems[0];
    }
    return null;
  };

  findOrgStructureChildren = (orgStructure, pid) => {
    const filteredItems = _.filter(orgStructure, item => `${item.parentOrgid}` === `${pid}`);
    return filteredItems;
  };

  onChangeTabs = key => {
    const { level1, level2, project } = this.props.match.params;
    switch (key) {
      case 'group_approve_version': // 跳转集团审批版
        router.push(`/ValueManage/PriceManage/GroupApproveVersion/${level1}/${level2}/${project}`);
        break;
      case 'overall_price': // 跳转全盘货值
        router.push(`/ValueManage/PriceManage/EntireProject/${level1}/${level2}/${project}`);
        break;
      default: {
        // 跳转分期
        // 根据价格管理版本当前分期最新版本信息 跳转分期最新的版本 默认跳转  investment 版本
        apis
          .priceNewestVersionUsingGET({
            params: {
              projectFid: key,
            },
          })
          .then(data => {
            if (data && data.data) {
              // 有数据跳转到全盘货值
              const versions = ['investment', 'strategy', 'pricing', 'dynamic'];
              const version = _.filter(versions, x => x === data.data.versionType)[0];
              if (version) {
                router.replace(
                  `/ValueManage/PriceManage/${level1}/${level2}/${project}/${key}/${version}/${data.data.versionId}`,
                );
              } else {
                router.replace(
                  `/ValueManage/PriceManage/${level1}/${level2}/${project}/${key}/investment`,
                );
              }
            } else {
              router.replace(
                `/ValueManage/PriceManage/${level1}/${level2}/${project}/${key}/investment`,
              );
            }
          });
      }
    }
  };

  render() {
    let activeKey = '';
    const { project, stage } = this.props.match.params;
    let { stages } = this.state;
    stages = _.orderBy(stages, ['orgorder'], ['asc']);
    if (this.props.location.pathname.search('EntireProject') > -1) {
      activeKey = 'overall_price';
    } else if (this.props.location.pathname.search('GroupApproveVersion') > -1) {
      activeKey = 'group_approve_version';
    } else if (stage) {
      activeKey = stage;
    } else {
      activeKey = 'overall_price';
    }
    if (!_.isEmpty(this.props.orgStructure.data)) {
      return project ? (
        <div className="header-stages">
          <Tabs
            size="large"
            activeKey={activeKey}
            defaultActiveKey="1"
            animated={false}
            onChange={this.onChangeTabs}
          >
            {/* 新增集团审批版页签 */}
            <TabPane tab="集团审批版" key="group_approve_version" />
            <TabPane tab="全盘货值" key="overall_price" />
            {stages && stages.length
              ? stages.map(value => (
                  <TabPane tab={`${value.orgname}`} key={value.orgdepid}></TabPane>
                ))
              : null}
          </Tabs>
        </div>
      ) : null;
    }
    return null;
  }
}
